package com.example.myapplication.ui.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.myapplication.Adapter.ChatItemAdapter;
import com.example.myapplication.Bean.ChatItem;
import com.example.myapplication.Bean.Msg;
import com.example.myapplication.Bean.User;
import com.example.myapplication.ChatAcvicity;
import com.example.myapplication.R;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private ListView chatitemlv;
    ChatItemAdapter adapter;
    List<ChatItem> chatItemList = new ArrayList<>();
    List<User> userList = new ArrayList<>();
    List<Msg> msgList = new ArrayList<>();
    int myid;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        initData();
        setListener();
        return view;
    }

    private void setListener() {//监听
//        点击聊天列表跳转到聊天界面
        chatitemlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                SharedPreferences sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                User user = userList.get(i);
                editor.putInt("friend", user.getId()); //获取id
                editor.putString("friendname", user.getName());//获取用户名
                editor.putInt("friendimg", user.getUserimg());//获取头像
                editor.apply();

                Intent intent = new Intent(getContext(), ChatAcvicity.class);
                startActivity(intent);
//                更新视图
                adapter.notifyDataSetChanged();

            }
        });

    }

    private void initData() {//数据
        SharedPreferences sp = getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        myid = sp.getInt("myid", -1);

        userList = LitePal.where("id!=?", myid + "").find(User.class);
        for (User u : userList) {
            msgList = getChatContent(u);
            if (msgList.size() != 0) {
                Msg m = msgList.get(msgList.size() - 1);
                //获取的最后一条应该是size-1   ，获取最后一条消息
                ChatItem chatItem = new ChatItem(u.getUserimg(), u.getName(), m.getContent(), m.getDate());
                chatItemList.add(chatItem);
                /*填充到 视图中 */

            }
        }
    }

    private List<Msg> getChatContent(User u) {
        List<Msg> msgs = new ArrayList<>();
        for (Msg m : LitePal.findAll(Msg.class)) {
            //如果我的id
            boolean a = myid == m.getReceive_id() && u.getId() == m.getSend_id();
            boolean b = u.getId() == m.getReceive_id() && myid == m.getSend_id();
            if (a || b) {
                msgs.add(m);
            }
        }
        return msgs;
    }

    private void initView(View view) {//实例化//
        chatitemlv = view.findViewById(R.id.chatitmlv);
        adapter = new ChatItemAdapter(getContext(), R.layout.chat_item, chatItemList);
        chatitemlv.setAdapter(adapter);


    }


}